var fs = require('fs');
var path = require('path');
var DxfStruct = require('./DxfStruct2');
var DxfParser = require('../');

var DXF_FILE_PATH = path.join(__dirname, 'data', 'tg.dxf');
var OUTPUT_FILE_NAME = "cad.json";

var fileStream = fs.createReadStream(DXF_FILE_PATH, { encoding: 'utf8' });
var parser = new DxfParser();
var layer_color = {}

parser.parseStream(fileStream, function (err, dxf) {
    if (err) return console.error(err.stack);
    //fs.writeFileSync(OUTPUT_FILE_NAME, JSON.stringify(dxf, null, 3));
    dxf.sys = { start: 513, points: [], dn: 400 }
    //console.log(dxf.tables.layer.layers)
    //return 
    dxfstruct = new DxfStruct(dxf)
    var blocks = ['gc304', 'gc178', 'gc214', 'GC026', 'gc104', 'GC101', 'GC021', 'GC023', 'GC020', 'gc309', 'GC030',
        'gc022', 'GC103', 'gc019', 'gc029', 'bc128', 'gc302', 'gc075', 'gc310', 'gc203', 'GC105',
        'gc106', '47.00-99.00', '*D91', 'DLSS', 'JMD', 'SXSS', '给水']
    var layers = ['给水','DLSS','SXSS','给水线注记']//'DLSS','JMD','SXSS','给水内业点号','给水线注记'//11382189
    var merge = ['DLSS','SXSS']

    var newLayer = {
        layer: ['给水','JMD','DLSS','SXSS'],
        //layer: ['给水'],
        blocks: ['gc304', 'gc178', 'gc214', 'GC026', 'gc104', 'GC101', 'GC021', 'GC023', 'GC020', 'gc309', 'GC030',
        'gc022', 'GC103', 'gc019', 'gc029', 'bc128', 'gc302', 'gc075', 'gc310', 'gc203', 'GC105',
        'gc106', '47.00-99.00', '*D91', 'DLSS', 'JMD', 'SXSS', '给水'],
        merge:['JMD','DLSS','SXSS','DN80'],
        clayer: {
            '给水': {
                '255': 'DN80',
                '1271604': 'DN100',
                '10000588': 'DN150',
                '10033950': 'DN600',
                '13461800': 'DN300',
                '13737159': 'DN200',
                '16698470': 'DN250',
                '16711680': 'DN400'
            }
        },
        tlayers: {
            'JMD': { c: '11382189', b: false },
            'DLSS': { c: '8684676', b: false },
            'SXSS': { c: '8388511', b: false },
            '给水线注记': { c: '255', b: false },
           
            'DN80': { c: '255', b: false },
            'DN100': { c: '1271604', b: true },
            'DN150': { c: '10000588', b: true },
            'DN200': { c: '13737159', b: true },
            'DN250': { c: '16698470', b: true },
            'DN300': { c: '13461800', b: true },
            'DN400': { c: '16711680', b: true },
            'DN600': { c: '10033950', b: true },
        },
    }
    
    dxfstruct.setShowLayers(newLayer)
    dxfstruct.resetDxf('user')

    console.log('dxfstruct',dxfstruct.dxf.entities.length)
    fs.writeFileSync(OUTPUT_FILE_NAME, JSON.stringify(dxfstruct.dxf));
    //console.log(dxfstruct.getDxfStruct())
});